<?php
use \GatewayWorker\Lib\Gateway;

//重新登录（不是断网重连），进入旧房间：re enter room
class reenterroom extends cmdbase
{
	public static $MaxNum = 8;
	public function onCmd($msg){
		$group_mgr = $GLOBALS['GroupMgr'];
		$group_id = $msg['room_id'];
		if($group_id < 0){
			//非法房号
	 		$msg_body = array(
	            'ret'=> NetErrorCode::$Error,
	            "room_id" => $group_id,
	            'reason'=>"非法房号$group_id",
	        );
	 		return $msg_body;

		}else{
	
			$client_count = Gateway::getClientCountByGroup($group_id);
			if($client_count < 1){
				//找不到房间，需要报错给客户端
				$msg_body = array(
		            'ret'=> NetErrorCode::$Error,
		            "room_id" => $group_id,
		            'reason'=>"房间人数不足:$client_count",
		        );
		 		return $msg_body;
			}else if($client_count > match::$MaxNum - 1){
				//房间人数已经满了
				$msg_body = array(
		            'ret'=> NetErrorCode::$Error,
		            "room_id" => $group_id,
		            'reason'=>"房间人数过多:$client_count",
		        );
		 		return $msg_body;
			}
		}

		//致命错误：找不到人
		$person_wrap = $group_mgr->getBattlePersonWrap($group_id);
		if(!isset($person_wrap)){
			//非法房号
	 		$msg_body = array(
	            'ret'=> NetErrorCode::$Error,
	            "room_id" => $group_id,
	            'reason'=>"非法房号$group_id,找不到剩余的人",
	        );
	 		return $msg_body;
		}

		//加入组
		$client_id = $this->getCurClientId();
		$group_mgr->joinGroup($client_id, $group_id);

		//发送信息
		$this->sendToCurrentClient("notify_enterbattle", $person_wrap);
		//广播
		$user_id = Gateway::getUidByClientId($client_id);
		$this->sendToGroup("notify_enterroom", array('uid'=>$user_id,'num' => Gateway::getClientCountByGroup($group_id)));

 		$msg_body = array(
            'ret'=> NetErrorCode::$None,
            "room_id" => $group_id,
            //'reason'=>"success",
        );
 		return $msg_body;
	}
}